package com.poetize.plus.entity.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.poetize.plus.entity.po.Article;
import io.swagger.v3.oas.annotations.media.Schema;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author zuosy
 * @Date 2024/3/30 15:50
 **/
public record ArticleVO(
        @Schema(description = "id")
        Long id,

        @Schema(description = "作者")
        String createBy,

        @Schema(description = "标题")
        String articleTitle,

        @Schema(description = "分类")
        Long sortId,

        @Schema(description = "标签")
        Long labelId,

        @Schema(description = "文章内容")
        String articleContent,

        @Schema(description = "浏览量")
        Integer viewCount,

        @Schema(description = "点赞数")
        Long likeCount,

        @Schema(description = "是否可见")
        Boolean viewStatus,

        @Schema(description = "是否加密")
        Boolean encoderStatus,

        @Schema(description = "密码")
        String password,

        @Schema(description = "封面")
        String articleCover,

        @Schema(description = "是否启用评论")
        Boolean commentStatus,

        @Schema(description = "是否推荐")
        Boolean recommendStatus,

        @Schema(description = "评论数")
        Long commentCount,

        @Schema(description = "创建时间")
        @JsonFormat(pattern = "yyyy-MM-dd")
        LocalDateTime createTime,

        @Schema(description = "更新时间")
        @JsonFormat(pattern = "yyyy-MM-dd")
        LocalDateTime updateTime
) {

        /**
         * 实体类集合转vo集合
         * @param articleList
         * @return
         */
        public static List<ArticleVO> convertList(List<Article> articleList){
                List<ArticleVO> result = new ArrayList<>();
                articleList.forEach(article -> result.add(convert(article, 0L)));
                return result;
        }

        /**
         * 实体类转vo
         * @param article
         * @return
         */
        public static ArticleVO convert(Article article, Long commentCount){
                return new ArticleVO(
                        article.getId(),
                        article.getCreateBy(),
                        article.getArticleTitle(),
                        article.getSortId(),
                        article.getLabelId(),
                        article.getArticleContent(),
                        article.getViewCount(),
                        0L,
                        article.getViewStatus(),
                        article.getEncoderStatus(),
                        article.getPassword(),
                        article.getArticleCover(),
                        article.getCommentStatus(),
                        article.getRecommendStatus(),
                        commentCount,
                        article.getCreateTime(),
                        article.getUpdateTime());
        }
}
